package org.grenobus;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.TabActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TwoLineListItem;

public class Grenobus extends TabActivity implements TabHost.TabContentFactory {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        TabHost tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("favorites")
                .setIndicator("Favoris", getResources().getDrawable(R.drawable.star))
                .setContent(this));
        tabHost.addTab(tabHost.newTabSpec("lines")
                .setIndicator("Par lignes", getResources().getDrawable(R.drawable.bus))
                .setContent(this));
        tabHost.addTab(tabHost.newTabSpec("address")
                .setIndicator("Par adresse", getResources().getDrawable(R.drawable.compass))
                .setContent(this));
	}

	class MyAdapter extends BaseAdapter {

		String items[] = new String[] { "Arret1", "Ligne1", "Arret2", "Ligne2", "Arret3", "Ligne3", "Arret4", "Ligne4", "Arret5", "Ligne5" };
		Context context;
		
		public MyAdapter(Context c) {
			context = c;
		}
		
		public int getCount() {
			return items.length/2;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			TwoLineListItem item = (TwoLineListItem)LayoutInflater.from(context).inflate(android.R.layout.simple_expandable_list_item_2, null, false);

			TextView t = (TextView)item.findViewById(android.R.id.text1); 
			t.setText(items[position*2]);
			t = (TextView)item.findViewById(android.R.id.text2); 
			t.setText(items[position*2+1]);
			return item;
		}
		
	};

	private static ExecutorService threadPool = Executors.newCachedThreadPool();
	
	static public ExecutorService getThreadPool() {
		return threadPool;
	}
		
	public View createTabContent(String tag) {
		if ("favorites".equals(tag)) {
			ListView l = new ListView(this);

	        l.setAdapter(new MyAdapter(this));
	        return l;
		} else {
			return new LinesView(this);
		}
	}
}
